<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class IndentController extends Controller
{
    //订单确认
    public function confirm(Request $request)
    {
        if (!$request->session()->has('user'))
        {
            return redirect('/home/login/login') -> with(['info' => '请登录']);
        }
        $uid = session('user') -> id;
        if ($request -> flag) {
            $shopcar = DB::table('shopcar')->where('uid', $uid)->first();
            foreach ($shopcar as $key => $value) {
                $data[$key] = $value;
            }
        } else {
            $data = $request -> except('_token');
        }
        $data = $request -> except('_token');
        $res[0] = DB::table('receiver') -> where('uid',$uid) ->where('selected',1) -> first();
        if($res[0])
        {
            $res1 = DB::table('receiver') -> where('uid',$uid) ->where('selected',0) -> get();
            if($res1)
            {

                foreach($res1 as $k => $v)
                {
                    static $i = 1;
                    $res[$i] = $v;
                    $i++;
                }
            }

            session(['res' => $res[0]]);
            session(['data' => $data]);
            return view('/home/indent/confirm') -> with(['data' => $data, 'res' => $res]);
        }else
        {
            
            $res = DB::table('receiver') -> where('uid',$uid)  -> get();
            if($res)
            {
                $ress = $res[0];
                session(['res' => $ress]);
                session(['data' => $data]);
                return view('/home/indent/confirm') -> with(['data' => $data, 'res' => $res]);
            }
            return back();
        }
    }
    //订单确认存收货地址session
    public function address($id)
    {
         $res = DB::table('receiver') -> where('id',$id) -> first();
        if($res)
        {
            session(['res' => $res]);
             return 1;
         }else
         {
             return 2;
         }

    }
    //提交订单
    public function doindent()
    {
        if (!session()->has('user'))
        {
            return redirect('/home/login/login') -> with(['info' => '请登录']);
        }
        if (!session()->has('res'))
        {
            return redirect('/home/indent/confirm') -> with(['info' => '重新确认订单']);
        }
        if (!session()->has('data'))
        {
            return redirect('/home/indent/confirm') -> with(['info' => '重新确认订单']);
        }
        $time = time();
        $arr['uaddress'] = session('res') -> id;
        $arr['uid'] = session('user') -> id;
        $arr['goodsid'] = session('data')['goodsid'];
        $arr['number'] = session('data')['number'];
        if(!empty(session('data')['color']))
        {
            $arr['color'] = session('data')['color'];
        }
        if(!empty(session('data')['rom']))
        {
            $arr['rom'] = session('data')['rom'];
        }
        if(!empty(session('data')['system']))
        {
            $arr['system'] = session('data')['system'];
        }
        if(!empty(session('data')['combo']))
        {
            $arr['combo'] = session('data')['combo'];
        }
        $arr['time'] = $time;
        $arr['order_number'] = '1024'.$time;

        $res = DB::table('indent') -> insert($arr);
        if($res)
        {
            session()->forget('res');
            session()->forget('data');
            return redirect('/home/indent/index') -> with(['info' => '全部订单','title' => '全部订单']);
        }else
        {
            return back() -> with(['info' => '提交失败']);
        }
    }
    //全部订单页面
    public function index()
    {
        $uid = session('user') -> id;
        $data = DB::table('indent as r1')
        -> leftJoin('goods as r2','r1.goodsid','=','r2.id')
        -> select('r1.*','r2.goodsname','r2.price','r2.goodspic')
        -> where('uid', $uid)
        -> orderBy('time','desc')
        -> get();
        foreach($data as $k => $v)
        {
            $v -> time = date("Y-m-d H:i:s",$v -> time);
        }
        return view('/home/indent/index') -> with(['data' => $data, 'info' => '全部订单']);
    }
    //待支付
    public function is_pay()
    {
        $uid = session('user') -> id;
        $data = DB::table('indent as r1')
        -> leftJoin('goods as r2','r1.goodsid','=','r2.id')
        -> select('r1.*','r2.goodsname','r2.price','r2.goodspic')
        -> where('uid', $uid)
        -> where('is_pay',0)
        -> orderBy('time','desc')
        -> get();
                foreach($data as $k => $v)
            {
                $v -> time = date("Y-m-d H:i:s",$v -> time);
            }

        return view('/home/indent/index') -> with(['data' => $data,'info' => '待支付']);
    }
    //待发货
    public function is_send()
    {
        $uid = session('user') -> id;
        $data = DB::table('indent as r1')
        -> leftJoin('goods as r2','r1.goodsid','=','r2.id')
        -> select('r1.*','r2.goodsname','r2.price','r2.goodspic')
        -> where('uid', $uid)
        -> where('is_pay',1)
        -> where('is_send',0)
        -> orderBy('time','desc')
        -> get();
        foreach($data as $k => $v)
        {
            $v -> time = date("Y-m-d H:i:s",$v -> time);
        }
        return view('/home/indent/index') -> with(['data' => $data,'info' => '待发货']);

    }
    //待收货
    public function is_receive()
    {
        $uid = session('user') -> id;
        $data = DB::table('indent as r1')
        -> leftJoin('goods as r2','r1.goodsid','=','r2.id')
        -> select('r1.*','r2.goodsname','r2.price','r2.goodspic')
        -> where('uid', $uid)
        -> where('is_pay',1)
        -> where('is_send',1)
        -> where('is_receive',0)
        -> orderBy('time','desc')
        -> get();
        foreach($data as $k => $v)
            {
                $v -> time = date("Y-m-d H:i:s",$v -> time);
            }
        // dd($data);
        return view('/home/indent/index') -> with(['data' => $data,'info' => '待收货']);
    }
    //待评价
    public function is_comment()
    {
        $uid = session('user') -> id;
        $data = DB::table('indent as r1')
        -> leftJoin('goods as r2','r1.goodsid','=','r2.id')
        -> select('r1.*','r2.goodsname','r2.price','r2.goodspic')
        -> where('uid', $uid)
        -> where('is_pay',1)
        -> where('is_send',1)
        -> where('is_receive',1)
        -> where('is_comment',0)
        -> orderBy('time','desc')
        -> get();
        foreach($data as $k => $v)
            {
                $v -> time = date("Y-m-d H:i:s",$v -> time);
            }
        // dd($data);
        return view('/home/indent/index') -> with(['data' => $data,'info' => '待评价']);
    }
    public function pay($id)
    {
        $data = DB::table('indent') -> where('id', $id) -> first();
        $time = $data -> time;
        $data -> time = date('Y-m-d H:i:s', $time);
        $uaddress = $data -> uaddress;
        $goodsid = $data -> goodsid;
        $rec = DB::table('receiver') -> where('id', $uaddress) -> first();
        $goods = DB::table('goods') -> where('id', $goodsid) -> first();
        $paytime = time() + 1800;
        $data -> paytime = date('Y-m-d H:i:s', $paytime);
        return view('/home/indent/pay') -> with(['data' => $data,'rec' => $rec,'goods' => $goods]);
    }
    public function dopay($id)
    {
        $arr['is_pay'] = 1;
        $res = DB::table('indent') -> where('id',$id) -> update($arr);
        if($res)
        {
            return redirect('/home/indent/is_receive');
        }else
        {
            return back() -> with(['info' => '支付失败']);
        }
    }
    public function cancel($id)
    {
        $res = DB::table('indent') -> where('id', $id) -> delete($id);
        if($res)
        {
            return redirect('/home/indent/is_pay');
        }else
        {
            return back();
        }
    }
    public function edit($id)
    {
        $data = DB::table('indent') -> where('id',$id) -> first();
        $uid = session('user') -> id;
        $goodsid = $data -> goodsid;
        $goods = DB::table('goods') -> where('id',$goodsid) -> first();
        $goodsname = $goods -> goodsname;
        $price = $goods -> price;
        dd($goodsname);
        $res = DB::table('receiver') -> where('uid',$uid) -> get();
        return view('/home/indent/edit') -> with(['res' => $res,'data' => $data]);
    }
}
